//
//  BSTabBarController.m
//  baisibudejie
//
//  Created by 陈博文 on 15/12/17.
//  Copyright © 2015年 陈博文. All rights reserved.
//

#import "BSTabBarController.h"

#import "BSEssenceViewController.h"
#import "BSFriendTrendViewController.h"
#import "BSMeViewController.h"
#import "BSNewController.h"
#import "BSPublishViewController.h"
#import "BSTabBar.h"

#import "BSNavigationController.h"

#define BSColor(r,g,b) [UIColor colorWithRed: (r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:1];

#define BSTabButtonTitleFont [UIFont systemFontOfSize:12]

@implementation BSTabBarController


+ (void)load
{
    // 获取整个app中tabBarItem
    
    // appearanceWhenContainedIn:获取某个类下的UITabBarItem
    UITabBarItem *item =  [UITabBarItem appearanceWhenContainedIn:self, nil];
    
    // 1.UITabBarItem 可以使用appearance,只要遵守<UIAppearance>
    
    // 2.不是任何属性都可以使用appearance,只有标识了UI_APPEARANCE_SELECTOR这个宏的属性,才可以使用appearance修改
    //    item.title = @"我";
    
    // 3.通过appearance设置,是有条件,必须在控件显示之前设置
    
    
    // 选中
    // 设置选中标题文字颜色
    // 富文本字符串:颜色,字体,空心,阴影,图文混排
    // NSDictionary:描述字符串属性
    
    //选中状态
    NSMutableDictionary *attrSel = [NSMutableDictionary dictionary];
    // 颜色
    attrSel[NSFontAttributeName] = BSTabButtonTitleFont;
    attrSel[NSForegroundColorAttributeName] = BSColor(64, 64, 64);
    [item setTitleTextAttributes:attrSel forState:UIControlStateSelected];
    
    // 设置tabBarButtin字体大小: 必须设置正常状态下,如果不设置,就会不好使.
    NSMutableDictionary *attrNor = [NSMutableDictionary dictionary];
    // 颜色
    attrNor[NSFontAttributeName] = BSTabButtonTitleFont;
    attrNor[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    [item setTitleTextAttributes:attrNor forState:UIControlStateNormal];
   
}



-(void)viewDidLoad{
    [super viewDidLoad];
    
    [self setUpChildViewController];
    [self setUpTitleAndImage];
   
    
    //在这里还没有加载出来,可以在这里进行设置
//        NSLog(@"%@",self.tabBar.items);
    //tabBar 是 readOnly 所以需要进行 KVC 赋值
    
    BSTabBar *tabBar = [[BSTabBar alloc] init];
    //
    //    // readonly:不会生成set方法,生成get方法还要下划线的成员属性
     [self setValue:tabBar forKeyPath:@"tabBar"];

}

#pragma mark ---------------
#pragma mark - 重写addChildViewController方法
-(void)addChildViewController:(UIViewController *)childController{
  
    
    BSNavigationController *nav = [[BSNavigationController alloc]initWithRootViewController:childController];
    
    [super addChildViewController:nav];
}

-(void)setUpChildViewController{
    //2.设置根控制器
   
    //设置UITabBar上的按钮内容,UITabBar上的按钮由对应子控制器的UITabBarItem属性
    //精华
    // 以后如果当前控制器不是tabBarController的子控制器,一定要记得设置图片,如果不设置,也设置不了文字.
    BSEssenceViewController *essence = [[BSEssenceViewController alloc]init];

    [self addChildViewController:essence];
    
    //最新
    BSNewController *new = [[BSNewController alloc]init];
    [self addChildViewController:new];
    
    //发布
    
    /*
    BSPublishViewController *public = [[BSPublishViewController alloc]init];
    //    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:public];
    [self addChildViewController:public];
    */
    //关注
    BSFriendTrendViewController *friendTrend = [[BSFriendTrendViewController alloc]init];

    [self addChildViewController:friendTrend];
    
    //我
//    BSMeViewController *me = [[BSMeViewController alloc]init];
    UIStoryboard *stroryboard = [UIStoryboard storyboardWithName:NSStringFromClass([BSMeViewController class]) bundle:nil];
    
    BSMeViewController *me = [stroryboard instantiateInitialViewController];
    
    [self addChildViewController:me];

}

/**
 *  谁会调用这个方法,这是一个对象方法,因此这个类产生的一个对象会过来调用这个方法
 *  这个对象由 Appdelegate 中创建的对象来调用
 *
 */

-(void)setUpTitleAndImage{
    //设置图片和标题
    
    // 2.2 设置UITabBar上的按钮,UITabBar上的按钮由对应子控制器的UITabBarItem属性
    // 精华
    UIViewController *vc0 = self.childViewControllers[0];
    
    vc0.tabBarItem.title = @"精华";
    vc0.tabBarItem.image = [UIImage imageNameWithOriginalMode:@"tabBar_essence_icon"];
    vc0.tabBarItem.selectedImage = [UIImage imageNameWithOriginalMode:@"tabBar_essence_click_icon"];
    
    // 新帖
    UIViewController *vc1 = self.childViewControllers[1];
    
    vc1.tabBarItem.title = @"新帖";
    vc1.tabBarItem.image = [UIImage imageNameWithOriginalMode:@"tabBar_new_icon"];
    vc1.tabBarItem.selectedImage = [UIImage imageNameWithOriginalMode:@"tabBar_new_click_icon"];
    /*
    // 发布
     UIViewController *vc2 = self.childViewControllers[2];
    
    vc2.tabBarItem.image = [UIImage imageNameWithOriginalMode:@"tabBar_publish_icon"];
    vc2.tabBarItem.selectedImage = [UIImage imageNameWithOriginalMode:@"tabBar_publish_click_icon"];
    */
    // 关注
     UIViewController *vc3 = self.childViewControllers[2];
    
    vc3.tabBarItem.title = @"关注";
    vc3.tabBarItem.image = [UIImage imageNameWithOriginalMode:@"tabBar_friendTrends_icon"];
    vc3.tabBarItem.selectedImage = [UIImage imageNameWithOriginalMode:@"tabBar_friendTrends_click_icon"];

    // 我
    UIViewController *vc4 = self.childViewControllers[3];
    
    vc4.tabBarItem.title = @"我";
    vc4.tabBarItem.image = [UIImage imageNameWithOriginalMode:@"tabBar_me_icon"];
    vc4.tabBarItem.selectedImage = [UIImage imageNameWithOriginalMode:@"tabBar_me_click_icon"];
}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
//    NSLog(@"%@",self);
    //tabBarItem自己目前显示的这个,根据 vc 来设置
//    NSLog(@"%@",self.tabBarItem);
    //tabBar中有的 item 个数-------在这里面可以打印出来,表明已经设置过了,需要在这之前进行设置
//    NSLog(@"%@",self.tabBar.items);
    //当前控制器
//    NSLog(@"%@",self.tabBarController);

}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //当前控制器
//    NSLog(@"%@",self.tabBarController);
    
}

@end
